package com.hystrix;

import com.hystrix.collapser.MyHystrixCollapser;
import com.hystrix.command.CacheCommand;
import com.hystrix.entity.Person;
import com.netflix.config.ConfigurationManager;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixRequestCache;
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategyDefault;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * 请先启动当前项目的Spirng Boot
 * Created by earl on 2018/5/1.
 */
public class TestCache {


    public static void main(String [] args) throws ExecutionException, InterruptedException {
        testCache();
    }

    //测试缓存
    private static void testCache() throws ExecutionException, InterruptedException {
        // 初始化请求上下文
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        // 请求正常的服务
        String key = "cache-key";
        CacheCommand c1 = new CacheCommand(key);
        CacheCommand c2 = new CacheCommand(key);
        CacheCommand c3 = new CacheCommand(key);
        // 输出结果
        System.out.println(c1.execute() + "c1 是否读取缓存: " + c1.isResponseFromCache());
        System.out.println(c2.execute() + "c2 是否读取缓存: " + c2.isResponseFromCache());
        System.out.println(c3.execute() + "c3 是否读取缓存: " + c3.isResponseFromCache());
        // 获取缓存实例
        HystrixRequestCache cache = HystrixRequestCache.getInstance(
                HystrixCommandKey.Factory.asKey("MyCommandKey"),
                HystrixConcurrencyStrategyDefault.getInstance());
        // 清空缓存
        cache.clear(key);
        // 重新执行命令
        CacheCommand c4 = new CacheCommand(key);
        System.out.println(c4.execute() + "c4 是否读取缓存: " + c4.isResponseFromCache());

        context.shutdown();
    }




}
